package com.stone.cat.common.util;

import com.google.common.base.Preconditions;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by 喵 on 2018/2/5.
 */
public final class BeanUtil {

    /**
     * @param target
     * @param source
     */
    public static void copyProperties(Object target, Object source) {
        try {
            Preconditions.checkNotNull(source);
            BeanUtils.copyProperties(target, source);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param target
     * @param source
     * @param <T>
     * @param <S>
     */
    public static <T, S> void copyPropertiesList(List<T> target, List<S> source,Class<T> targetClass) {
        source.forEach(e -> {
            try {
                T t = targetClass.newInstance();
                copyProperties(t, e);
                target.add(t);
            } catch (InstantiationException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            }
        });
    }


}
